package Controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.ProductModel;
import POJO.UserPOJO;

/**
 * Servlet implementation class Product
 */
@WebServlet("/product")
public class Product extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Product() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserPOJO user  =  (UserPOJO)session.getAttribute("user");

		if(user != null)
		{
			//kiem tra phan quyen
			if(user.getType().equals("Merchandise") || user.getType().equals("System Admin")  )
			{
				request.setAttribute("do", "true");
			}
			request.setAttribute("menu", session.getAttribute("menu"));
			request.setAttribute("user", user);
			request.setAttribute("view", "Products");
			request.setAttribute("url", "product");
			if(request.getParameter("add")!= null)
			{
				request.setAttribute("action", "Add");
				RequestDispatcher rd = request.getRequestDispatcher("View/Add.jsp");
				rd.forward(request, response);
			}
			else if(request.getParameter("id")!= null)
			{
				request.setAttribute("action", "Add");
				request.setAttribute("id",request.getParameter("id"));
				RequestDispatcher rd = request.getRequestDispatcher("View/Add.jsp");
				rd.forward(request, response);
			}
			else
			{
				request.setAttribute("action", "View");
				RequestDispatcher rd = request.getRequestDispatcher("View/View.jsp");
				rd.forward(request, response);
			}
		}
		else
		{
			response.sendRedirect("login");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String choose=(String)request.getParameter("do");
		switch(choose)
		{
			case "delete":{DeleteProduct(request,response);break;}
			case "Add":{InsertProduct(request,response);break;}
			case"Edit":{UpdateProduct(request,response);break;}
			case "Search":{response.sendRedirect("product?key=" + request.getParameter("key"));break;}
		}
	}
	private void InsertProduct (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String name=request.getParameter("txtname");
		float price=Float.parseFloat(request.getParameter("txtprice"));
		int number=Integer.parseInt(request.getParameter("txtnumber"));
		int category=Integer.parseInt(request.getParameter("txtcategory"));
		String info=request.getParameter("txtdes");
		int kq=ProductModel.InsertProduct(name, price, number, category, info);
		HttpSession session = request.getSession();
		if(kq==1)
		{
			session.setAttribute("doSuscess", "Product added sucessfully");
		}
		else
		{
			session.setAttribute("doError", "Product added Error");
		}
		response.sendRedirect("product");
	}
	private void UpdateProduct (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String name=request.getParameter("txtname");
		float price=Float.parseFloat(request.getParameter("txtprice"));
		int number=Integer.parseInt(request.getParameter("txtnumber"));
		int category=Integer.parseInt(request.getParameter("txtcategory"));
		String info=request.getParameter("txtdes");
		String id=request.getParameter("idedit");
		int kq=ProductModel.UpdateProduct(name, price, number, category, info, id);
		HttpSession session = request.getSession();
	
			if(kq==1)
			{
				session.setAttribute("doSuscess", "Product updated sucessfully");
			}
			else
			{
				session.setAttribute("doError", "Product updated Error");
			}
		
		response.sendRedirect("product");
	}
	private void DeleteProduct (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		int id=Integer.parseInt(request.getParameter("id"));
		int kq=ProductModel.DeleteProduct(id);
		HttpSession session = request.getSession();
		if(kq==1)
		{
			session.setAttribute("doSuscess", "Product deleted sucessfully");
		}
		else
		{
			session.setAttribute("doError", "Product deleted Error");
			
		}
		response.sendRedirect("product");
	}
}
